﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace DataAccess.Service
{
    /// <summary>
    /// 业务基类
    /// </summary>
    public class BaseService
    {
        private string _message;

        /// <summary>
        /// 获取消息
        /// </summary>
        public string Message
        {
            get
            {
                if (!string.IsNullOrEmpty(_message)) { return _message; }
                if (JsonObject == null) { return string.Empty; }

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                var json = serializer.Serialize(JsonObject);
                var d = serializer.Deserialize(json, typeof(ServiceJsonData)) as ServiceJsonData;
                if (d == null) { return string.Empty; }
                return d.msg;
            }
            protected set { this._message = value; }
        }

        /// <summary>
        /// 获取Json对象
        /// </summary>
        public object JsonObject { get; protected set; }
        
        /// <summary>
        /// 外抛消息
        /// </summary>
        protected void OutMessage(string message) { this._message = message; }
        
        /// <summary>
        /// 外抛Json对象
        /// </summary>
        /// <param name="obj">对象</param>
        protected void OutJsonObject(object obj)
        {
            JsonObject = obj;
        }
        
        class ServiceJsonData
        {
            public string status { get; set; }
            public string msg { get; set; }
        }
    }
}
